package com.minyifang.wcspf.work.ui.user.activities;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.minyifang.wcspf.R;
import com.minyifang.wcspf.common.base.BaseMvpActivity;
import com.minyifang.wcspf.common.utils.CountDownTimerUtils;
import com.minyifang.wcspf.common.utils.LogUtil;
import com.minyifang.wcspf.common.utils.ToastHelper;
import com.minyifang.wcspf.work.dialog.VerifyCaptchaDialog;
import com.minyifang.wcspf.work.ui.user.model.CaptchaBean;
import com.minyifang.wcspf.work.model.HttpRespond;
import com.minyifang.wcspf.work.ui.user.presenter.ForgetPwdPresenter;
import com.minyifang.wcspf.work.ui.user.view.ForgetPwdView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 忘记密码
 * Created by LuoYaWei on 2018/6/14.
 */
public class ForgetPwdActivity extends BaseMvpActivity<ForgetPwdView, ForgetPwdPresenter> implements ForgetPwdView {

    @BindView(R.id.edt_phone)
    EditText edt_phone;
    @BindView(R.id.edt_captcha)
    EditText edt_captcha;
    @BindView(R.id.tv_captcha)
    TextView tv_captcha;

    private VerifyCaptchaDialog dialog;

    @Override
    protected ForgetPwdPresenter createPresenter() {
        return new ForgetPwdPresenter();
    }

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_forget_pwd;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.tv_captcha)
    void showCaptchaDialog() {
        String strPhone = edt_phone.getText().toString();
        if (TextUtils.isEmpty(strPhone)) {
            ToastHelper.getInstance(this).showWarn("请输入手机号");
            return;
        }
        mPresenter.getCaptchaUrl(strPhone);
    }


    @OnClick(R.id.btn_next)
    void btnNextClick() {
        String strPhone = edt_phone.getText().toString();
        if (TextUtils.isEmpty(strPhone)) {
            ToastHelper.getInstance(this).showWarn("请输入手机号");
            return;
        }

        String strCaptcha = edt_captcha.getText().toString();
        if (TextUtils.isEmpty(strCaptcha)) {
            ToastHelper.getInstance(this).showWarn("请输入验证码");
            return;
        }

        mPresenter.checkCaptcha(strPhone, strCaptcha);

    }

    @OnClick(R.id.tv_service)
    void gotoService() {
        startActivity(new Intent(this, ServiceActivity.class));
    }


    @Override
    public void showCaptchaUrl(HttpRespond<CaptchaBean> respond) {
        if (respond.result == 1) {
            CaptchaBean bean = respond.data;
            LogUtil.e(bean.captchaUrl);
            dialog = VerifyCaptchaDialog.newInstance(
                    edt_phone.getText().toString(), bean.captchaUrl, new VerifyCaptchaDialog.OnVerifyListener() {
                        @Override
                        public void onOkClick(String captcha) {
                            mPresenter.sendSMS(edt_phone.getText().toString(), captcha);
                        }
                    });
            dialog.show(getSupportFragmentManager(), "captcha");

        } else
            ToastHelper.getInstance(this).showWarn(respond.message);
    }

    @Override
    public void sendSMSRes(HttpRespond respond) {
        if (respond.result == 1) {
            ToastHelper.getInstance(this).showSucceed("短信发送成功");
            if (dialog != null) {
                dialog.dismiss();
            }
            CountDownTimerUtils countDownTimerUtils =
                    new CountDownTimerUtils(tv_captcha, 60000, 1000);
            countDownTimerUtils.start();
        } else {
            ToastHelper.getInstance(this).showWarn(respond.message);
            dialog.refreshCaptcha();
        }
    }

    @Override
    public void checkCaptchaRes(HttpRespond respond) {
        if (respond.result == 1) {
            Intent intent = new Intent(this, ForgetPwdNextActivity.class);
            intent.putExtra("phone", edt_phone.getText().toString().trim());
            startActivity(intent);
        } else
            ToastHelper.getInstance(this).showWarn(respond.message);
    }


    @Override
    public void showLoadingView() {

    }

    @Override
    public void hideLoadingView() {

    }

    @Override
    public void onNetworkError() {

    }

}
